//
//  RecordDataBase.m
//  geiTA
//
//  Created by 杨元龙 on 14-11-20.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "RecordDataBase.h"
#import "RecordModel.h"
#import "DB.h"
@implementation RecordDataBase
+(void)insertWithModel:(RecordModel *)model
{//获取数据库指针
    sqlite3 *db = [DB shareDataBase];
    
    NSString *sqlStr = [NSString stringWithFormat:@"insert into record(recordid,name,sex,remindDate,phone,relationship,note,photo) values (%ld,'%@','%@','%@','%@','%@','%@','%@')",(long)model.recordid, model.name, model.sex, model.remindDate, model.phone, model.relationship, model.note,model.photo];
    
    int result =sqlite3_exec(db, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"添加成功");
    }else
    {
        NSLog(@"添加失败");
    }
    NSLog(@"%d",result);
  
}
+ (void)deleteWithModel:(NSInteger)recordid
{
    sqlite3 *db = [DB shareDataBase];
    NSString *sqlStr = [NSString stringWithFormat:@"delete from record where recordid = %ld",(long)recordid];
    int result = sqlite3_exec(db, [sqlStr UTF8String], nil, nil, nil) ;
    
    if (result  == SQLITE_OK) {
        NSLog(@"删除成功");
    }else
    {
        NSLog(@"删除失败");
    }
    NSLog(@"%d",result);
}

+(void)updateWithModel:(RecordModel *)model
{
    sqlite3 *db = [DB shareDataBase];
    NSString * sqlStr = [NSString stringWithFormat:@"update record set name = '%@',sex= '%@',remindDate= '%@',phone= '%@',relationship= '%@',note= '%@',photo = '%@' where recordid = %ld" ,model.name, model.sex, model.remindDate, model.phone, model.relationship, model.note, model.photo, (long)model.recordid];
    int result = sqlite3_exec(db, [sqlStr UTF8String], nil, nil, nil);
    
    if (result  == SQLITE_OK) {
        NSLog(@"修改成功");
    }else
    {
        NSLog(@"修改失败");
    }
    NSLog(@"%d",result);
 
}

+ (NSMutableArray *)selectAll
{
    
    sqlite3 *db =[DB shareDataBase];
    
    sqlite3_stmt *stmt = nil;
    
    NSString *sqlStr = [NSString stringWithFormat:@"select * from record"];
    int result = sqlite3_prepare_v2(db, [sqlStr UTF8String], -1, &stmt, nil);
    NSMutableArray *array =[NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            int recordid = sqlite3_column_int(stmt, 0);
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            const unsigned char *sex = sqlite3_column_text(stmt, 2);
            const unsigned char *remindDate = sqlite3_column_text(stmt, 3);
            const unsigned char *note = sqlite3_column_text(stmt, 4);
            const unsigned char *photo = sqlite3_column_text(stmt, 5);
            const unsigned char *relationship =sqlite3_column_text(stmt, 6);
            NSString *name1 = [NSString stringWithUTF8String:(const char *)name];
            NSString *sex1 = [NSString stringWithUTF8String:(const char *)sex];
            NSString *remindDate1 = [NSString stringWithUTF8String:(const char *)remindDate];
            NSString *note1 = [NSString stringWithUTF8String:(const char *)note];
            NSString *photo1 = [NSString stringWithUTF8String:(const char *)photo];
            NSString *relationship1 = [NSString stringWithUTF8String:(const char *)relationship];

            
            RecordModel *model = [RecordModel ReconrdModelWithId:recordid name:name1 sex:sex1 remindDate:remindDate1 phone:photo1 relationship:relationship1 note:note1 photo:photo1];
            
            [array addObject:model];
            
        }
    }
    sqlite3_finalize(stmt);
    return array;
    
}


+ (RecordModel*)selectWith:(NSInteger)recordid
{
    
    sqlite3 *db =[DB shareDataBase];
    
    sqlite3_stmt *stmt = nil;
    
    NSString *sqlStr = [NSString stringWithFormat:@"select * from record where recordid = %ld",recordid];
    int result = sqlite3_prepare_v2(db, [sqlStr UTF8String], -1, &stmt, nil);
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            int recordid = sqlite3_column_int(stmt, 0);
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            const unsigned char *sex = sqlite3_column_text(stmt, 2);
            const unsigned char *remindDate = sqlite3_column_text(stmt, 3);
            const unsigned char *note = sqlite3_column_text(stmt, 6);
            const unsigned char *phone = sqlite3_column_text(stmt, 4);
            const unsigned char *relationship =sqlite3_column_text(stmt, 5);
            const unsigned char *photo = sqlite3_column_text(stmt, 7);
            NSString *name1 = [NSString stringWithUTF8String:(const char *)name];
            NSString *sex1 = [NSString stringWithUTF8String:(const char *)sex];
            NSString *remindDate1 = [NSString stringWithUTF8String:(const char *)remindDate];
            NSString *note1 = [NSString stringWithUTF8String:(const char *)note];
            NSString *phone1 = [NSString stringWithUTF8String:(const char *)phone];
            NSString *relationship1 = [NSString stringWithUTF8String:(const char *)relationship];
            NSString *photo1 = [NSString stringWithUTF8String:(const char *)photo];
            
            
            RecordModel *model = [RecordModel ReconrdModelWithId:recordid name:name1 sex:sex1 remindDate:remindDate1 phone:phone1 relationship:relationship1 note:note1 photo:photo1];
            sqlite3_finalize(stmt);
            return model ;

         
            
        }
    }
    sqlite3_finalize(stmt);
    return nil ;

}



@end
